﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;


using ESOL.Common.Entity;
using ESOL.Business.Chk;
using ESOL.Business.Mst;
using ESOL.WinUI.Pur;

namespace ESOL.WinUI.Qc
{
    public partial class frmManageIncomingQc : BaseForm.frmBaseMdiBusinessForm
    {
        public frmManageIncomingQc()
        {
            InitializeComponent();
        }

        private void BindComobox()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("value", typeof(int));
            dt.Rows.Add("", null);
            dt.Rows.Add("通过", 0);
            dt.Rows.Add("不良", 1);
            cmbResultTag.DataSource = dt;
            cmbResultTag.DisplayMember = "name";
            cmbResultTag.ValueMember = "value";

            repositoryItemGridLookUpEdit1.DataSource = dt;
            repositoryItemGridLookUpEdit1.DisplayMember = "name";
            repositoryItemGridLookUpEdit1.ValueMember = "value";

            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", true);
            DataTable dt1 = MstCommonItemBs.GetMstCommonItemDtByType("STATUS");
            repositoryItemGridLookUpEdit2.DataSource = dt1;
            repositoryItemGridLookUpEdit2.DisplayMember = "ITEM_NAME";
            repositoryItemGridLookUpEdit2.ValueMember = "ITEM_CODE";

        }
        private void SetField()
        {
            cmbResultTag.SetSelectedValue(1);
        }
        private string GetCondition()
        {
            string sCondition = "";
            if (string.IsNullOrEmpty(txtIqcOrder.Text) == false)
            {
                sCondition += "and qio.ORDER_CODE like '" + txtIqcOrder.Text + "' ";
            }
            if (txtArrivedOrder.Tag != null)
            {
                sCondition += "and qio.PUR_ARRIVED_ID='" + ((PurPurchaseArrivedEntity)txtArrivedOrder.Tag).ArrivedId + "' ";
            }
            if (string.IsNullOrEmpty(cmbResultTag.SelectedValue.ToString()) == false)
            {
                sCondition += "and qio.RESULT_TAG='" + cmbResultTag.SelectedValue.ToString() + "' ";
            }
            if (string.IsNullOrEmpty(cmbStatus.SelectedValue.ToString()) == false)
            {
                sCondition += "and qio.STATUS='" + cmbStatus.SelectedValue.ToString() + "' ";
            }
            return sCondition;
        }
        private void BindGrid()
        {
            string sCondition = GetCondition();
            string sOrderBy = "order by qio.ORDER_CODE desc ";
            DataTable dt = QcIqcOrderBs.GetDtAllByCondition(sCondition, "", sOrderBy);
            gvMain.DataSource = dt;
        }
        private void BindDetailGrid()
        {
            if (mSelectedIqcOrder == null)
                return;
            DataTable dtIqcOrderItem = QcIqcOrderItemBs.GetDtIqcOrderItemByOrderID(mSelectedIqcOrder.OrderId);
            gvDetail.DataSource = dtIqcOrderItem;
        }
        private void SetButton()
        {
            if (mSelectedIqcOrder == null)
                return;
            switch (mSelectedIqcOrder.Status)
            {
                case "STATUS01":
                    btnModify.Enabled = true;
                    btnDelete.Enabled = true;
                    btnSubmit.Enabled = true;
                    btnApprove.Enabled = false;
                    btnConfirm.Enabled = false;
                    break;
                case "STATUS02":
                    btnModify.Enabled = false;
                    btnDelete.Enabled = false;
                    btnSubmit.Enabled = false;
                    btnApprove.Enabled = true;
                    btnConfirm.Enabled = false;
                    break;
                case "STATUS03":
                    btnModify.Enabled = false;
                    btnDelete.Enabled = false;
                    btnSubmit.Enabled = false;
                    btnApprove.Enabled = false;
                    btnConfirm.Enabled = true;
                    break;
                case "STATUS04":
                    btnModify.Enabled = true;
                    btnDelete.Enabled = false;
                    btnSubmit.Enabled = false;
                    btnApprove.Enabled = false;
                    btnConfirm.Enabled = false;
                    break;
                case "STATUS05":
                    btnModify.Enabled = false;
                    btnDelete.Enabled = false;
                    btnSubmit.Enabled = false;
                    btnApprove.Enabled = false;
                    btnConfirm.Enabled = false;
                    break;
                case "STATUS06":
                    btnModify.Enabled = false;
                    btnDelete.Enabled = false;
                    btnSubmit.Enabled = false;
                    btnApprove.Enabled = false;
                    btnConfirm.Enabled = false;
                    break;
            }
        }

        private void frmIncomingQcManage_Load(object sender, EventArgs e)
        {
            BindComobox();
            SetField();
            BindGrid();
        }

        private QcIqcOrderEntity mSelectedIqcOrder;

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataTable dt = (DataTable)gvMain.DataSource;
            mSelectedIqcOrder = new QcIqcOrderEntity(gridView1.GetFocusedDataRow());
            BindDetailGrid();
            SetButton();
        }

        protected override void Click_View()
        {
            if (mSelectedIqcOrder == null)
            {
                MessageBox.Show("请选择一张质检单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainIncomingQc frm = new frmMaintainIncomingQc();
            frm.ActionType = "View";
            frm.SelectedIqcOrder = mSelectedIqcOrder;
            frm.ShowDialog();
        }
        protected override void Click_New()
        {
            frmMaintainIncomingQc frm = new frmMaintainIncomingQc();
            frm.ActionType = "New";
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGrid();
            }
        }
        protected override void Click_Modify()
        {
            if (mSelectedIqcOrder == null)
            {
                MessageBox.Show("请选择一张质检单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainIncomingQc frm = new frmMaintainIncomingQc();
            frm.ActionType = "Modify";
            frm.SelectedIqcOrder = mSelectedIqcOrder;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGrid();
            }
        }
        protected override void Click_Delete()
        {
            if (mSelectedIqcOrder == null)
            {
                MessageBox.Show("请选择一张质检单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (MessageBox.Show("确定要删除质检单？", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                mSelectedIqcOrder.Status = "STATUS05";
                if (QcIqcOrderBs.DeleteQcIqcOrder(mSelectedIqcOrder))
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindGrid();
                }
            }
        }
        protected override void Click_Submit()
        {
            if (mSelectedIqcOrder == null)
            {
                MessageBox.Show("请选择一张质检单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (MessageBox.Show("确定要送审？", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                mSelectedIqcOrder.Status = "STATUS02";
                if (QcIqcOrderBs.UpdateQcIqcOrder(mSelectedIqcOrder))
                {
                    MessageBox.Show("送审成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindGrid();
                }
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectedIqcOrder == null)
            {
                MessageBox.Show("请选择一张待审核的质检单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainIncomingQcApprove frm = new frmMaintainIncomingQcApprove(mSelectedIqcOrder);
            frm.ActionType = "New";
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGrid();
            }
        }
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (mSelectedIqcOrder == null)
            {
                MessageBox.Show("请选择一张待审核的质检单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainIncomingQcApprove frm = new frmMaintainIncomingQcApprove(mSelectedIqcOrder);
            frm.ActionType = "Confirm";
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGrid();
            }
        }

        protected override void Click_Query()
        {
            BindGrid();
        }

        private void txtArrivedOrder_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainPurchaseArrivedQuery frm = new frmMaintainPurchaseArrivedQuery();
            frm.ShowDialog();
            if (frm.DialogResult == DialogResult.OK && frm.SelectedPurchaseArrived != null)
            {
                txtArrivedOrder.Text = frm.SelectedPurchaseArrived.ArrivedCode;
                txtArrivedOrder.Tag = frm.SelectedPurchaseArrived;
            }
            else
            {
                txtArrivedOrder.Text = "";
                txtArrivedOrder.Tag = null;
            }

        }

    }
}